package scala.collection.change

/**
 * 集合高级函数
 */
object ListTest3 {
  def main(args: Array[String]): Unit = {
    val list = List(1, 2, 3, 4, 5, 6, 7, 8, 9)
    val nestedList = List(List(1, 2, 3), List(4, 5, 6), List(7, 8, 9))
    val wordList = List("Hello world", "Hello atguigu", "hello scala")

    //    1.过滤
    println(list.filter(m => m > 3))

    //    2.转换映射
    println(list.map(m => m + 1))

    //    3.扁平化
    println(nestedList.flatten)

    //    4.扁平化+映射 注：flatMap相当于先进行map操作，再进行flatten操作
    println(wordList.flatMap(x => x.split(" ")))

    //    5.分组
    println(list.groupBy(x => x % 2))

    //    1. 将数据两两结合，实现运算规则
    println(list.reduce((x, y) => x - y))


    //    2.从源码的角度，reduce底层调用的其实就是reduceLeft
    val i1: Int = list.reduceLeft((x, y) => x - y)
    println(i1)

    //    ((4-3)-2-1)
    val i2: Int = list.reduceRight((x, y) => x - y)
    println(i2)


  }
}
